<script setup lang="js">
import { MYURL as URL } from '@/utils/express/express.js'
import Vrouter from "@/router"
import { onMounted, ref, inject, onBeforeUnmount } from "vue"
import { get_user_info } from '@/api/user/user.js'
import { userStore } from "@/stores/user.js"
import get_LV from '@/api/experience/experience.js'
const changeFriend = userStore()
changeFriend.getMyVideo()
let imgURL = `${URL}/public/icon/back_white.png`
const router = Vrouter
let lv_img = ref(`${URL}/public/icon/lv1.png`)
const go_change_imformation = () => {
    router.push({ path: 'changeimformation' })
}
const getback = () => {
    router.go(-1)
}
const top_name = ref('none')
const get_back_main = ref(null)
const back_image = ref(null)
const sendMessage = inject('change_message_state')

onMounted(() => {
    addEventListener('scroll', scrollStyle)
})
const scrollStyle = () => {
    if (document.body.scrollTop > 100 || document.documentElement.scrollTop > 100) {
        get_back_main.value.style.backgroundColor = `white`
        top_name.value = 'flex'
        back_image.value.style.backgroundColor = `rgba(0, 0, 0, 0)`
        imgURL = `${URL}/public/icon/back_black.png`
    } else {
        get_back_main.value.style.backgroundColor = `rgba(0, 0, 0, 0)`
        top_name.value = 'none'
        back_image.value.style.backgroundColor = `rgba(0, 0, 0, 0.23)`
        imgURL = `${URL}/public/icon/back_white.png`
    }
}
onBeforeUnmount(() => {
    removeEventListener('scroll', scrollStyle)
})

const manORgril = ref('man')
const manORgril_URL = ref(`${URL}/public/icon/${manORgril.value}.png`)
const detail_click = ref(false)
const detail_text = ref('详情')
const detail_btn = () => {
    if (detail_click.value) {
        detail_text.value = '详情'
        detail_click.value = false
    } else {
        detail_text.value = '收起'
        detail_click.value = true
    }
}
const exp_length = ref('0')
const person_imformation = ref({})

onMounted(() => {
    get_user_info().then((res) => {
        if (res.data.status === 0) {
            manORgril_URL.value = `${URL}/public/icon/${res.data.mes[0].sex}.png`
            person_imformation.value.sex = res.data.mes[0].sex
            res.data.mes[0].vip === '大会员' ? person_imformation.value.vip = true : person_imformation.value.vip = false
            person_imformation.value.username = res.data.mes[0].username
            person_imformation.value.good = res.data.mes[0].good
            person_imformation.value.about = res.data.mes[0].about
            res.data.mes[0].active === null ? person_imformation.value.active = 0 : person_imformation.value.active = res.data.mes[0].active
            res.data.mes[0].fans === null ? person_imformation.value.fans = 0 : person_imformation.value.fans = res.data.mes[0].fans.split(',').length
            res.data.mes[0].idol === null ? person_imformation.value.idol = 0 : person_imformation.value.idol = res.data.mes[0].idol.split(',').length
            person_imformation.value.profile = `${URL}/public/profiles/${res.data.mes[0].profile}`
            person_imformation.value.experience = res.data.mes[0].experience
            person_imformation.value.id = res.data.mes[0].id
            lv_img.value = `${URL}/public/icon/lv${get_LV(person_imformation.value.experience).LV}.png`
            person_imformation.value.experience_need = get_LV(person_imformation.value.experience).need
            isNaN(get_LV(person_imformation.value.experience).need) ? exp_length.value = 1 : exp_length.value = person_imformation.value.experience / get_LV(person_imformation.value.experience).need
        } else {
            router.push('my')
            sendMessage('登陆错误')
        }
    })
})
const goFan = (e) => {
    changeFriend.changeFriendState(e)
    router.push('/friend')
}
</script>
<template>
    <div style="background-color: white; position: relative;">
        <div class="get-back-main" ref="get_back_main">
            <img class="back-image" ref="back_image" :src="imgURL" @click="getback">
            <div :style="{ display: top_name }" class="title">
                <p>{{ person_imformation.username }}</p>
            </div>
        </div>
        <!-- <div class="getback-placehoder"></div> -->
        <div class="user-top-img">
            <img src="@/assets/img/personback.png">
        </div>
        <div class="user-imformation">
            <div><img :src="person_imformation.profile"></div>
            <div>
                <div class="person_num">
                    <div @click="goFan(false)">
                        <p>{{ person_imformation.fans }}</p>
                        <p>粉丝</p>
                    </div>
                    <p>|</p>
                    <div @click="goFan(true)">
                        <p>{{ person_imformation.idol }}</p>
                        <p>关注</p>
                    </div>
                    <p>|</p>
                    <div>
                        <p>{{ person_imformation.good }}</p>
                        <p>获赞</p>
                    </div>
                </div>
                <div class="change-imformation">
                    <p @click="go_change_imformation">编辑资料</p>
                </div>
            </div>
        </div>
        <div class="user-name">
            <div class="user-name-top">
                <p>{{ person_imformation.username }}</p>
                <p :class="{ big_vip: person_imformation.vip }">大会员</p>
            </div>
            <div class="user-experience">
                <div class="user-exp-top">
                    <img :src="lv_img">
                    <p>{{ person_imformation.experience }}/{{ person_imformation.experience_need }}</p>
                </div>
                <div class="user-exp-bottom">
                    <div :style="{ width: exp_length * 100 + '%' }"></div>
                </div>
            </div>
        </div>
        <div class="user-about">
            <p :class="{ about_hideen: !detail_click }">{{ person_imformation.about }}</p>
            <span @click="detail_btn">{{ detail_text }}</span>
            <div v-show="detail_click">
                <img v-show="person_imformation.sex === 'man' || person_imformation.sex === 'gril'" :src="manORgril_URL">
                <p v-show="person_imformation.sex != 'man' && person_imformation.sex != 'gril'" class="sex_label">{{
                    person_imformation.sex === 'secret' ? '保密' : person_imformation.sex }}</p>
                <div>
                    <img src="@/assets/img/uid.png" alt="">
                    <p>{{ person_imformation.id }}</p>
                </div>
            </div>
        </div>
        <div class="title_nav">
            <router-link replace to="user_detail">主页</router-link>
            <router-link replace to="user_add">投稿</router-link>
            <router-link replace to="user_good">点赞</router-link>
            <router-link replace to="user_star">收藏</router-link>
        </div>
    </div>
    <RouterView />
</template>
<style scoped lang="less">
.sex_label {
    color: #979ba1;
    font-size: 9px;
    padding: 0 2px;
    display: flex;
    align-items: center;
    justify-content: center;
    line-height: 14px;
    border: .6px solid #979ba1;
}

.big_vip {
    background-color: var(--bilibili-pink) !important;
}

.get-back-main {
    background-color: rgba(0, 0, 0, 0);
    z-index: 100;

    p {
        color: #605f64;
    }

    .back-image {
        width: 30px;
        padding: 5px;
        background-color: rgba(0, 0, 0, 0.23);
        border-radius: 50%;
    }
}

.user-top-img {
    img {
        width: 100vw;
    }
}


.user-imformation {
    padding: 0 10px 0 20px;
    display: flex;

    >div:last-child {
        width: 60%;
        margin-left: auto;
    }

    >div {
        display: flex;
        flex-direction: column;

        .change-imformation {
            p {
                text-align: center;
                width: 100%;
                color: #f68db5;
                padding: 3px 0;
                border: #f68db5 1px solid;
                border-radius: 5px;
            }
        }

        img {
            position: relative;
            top: -30px;
            height: 90px;
            width: 90px;
            border-radius: 50%;
            object-fit: cover;
            border: 2px white solid;
        }

        >div {
            display: flex;
            justify-content: space-between;
            align-items: center;

            >p {
                color: #9e9da2;
            }

            >div {
                display: flex;
                flex-direction: column;
                align-items: center;

                p:last-child {
                    font-size: 12px;
                    color: #979ba1;
                }
            }
        }

        p {
            margin: auto;
        }
    }
}

.user-name {
    display: inline-flex;
    flex-direction: column;
    padding: 0 10px;

    .user-name-top {
        display: flex;
        gap: 5px;
        align-items: center;

        >p:nth-child(1) {
            font-size: 16px;
            font-weight: 600;
        }

        >p:nth-child(2) {
            color: white;
            background-color: #aaaaaa;
            font-size: 11px;
            padding: 0 3px;
            border-radius: 10px;
            font-weight: 600;
        }
    }

    .user-experience {
        .user-exp-top {
            display: flex;
            align-items: center;
            gap: 8px;

            img {
                width: 20px;
            }

            p {
                color: #95999f;
                font-size: 10px;
                font-weight: 600;
            }
        }

        .user-exp-bottom {
            width: 100%;
            height: 3px;
            border-radius: 10px;
            background-color: #95999f;
            overflow: hidden;

            >div {
                height: 100%;
                background-color: #d57545;
            }
        }
    }
}

.user-about {
    width: 100%;
    margin-top: 5px;
    padding: 0 10px 10px 10px;
    position: relative;

    >p {
        width: 90%;
        font-size: 12px;
        color: #95999f;
        overflow: auto;
        text-overflow: ellipsis;
        white-space: wrap;
    }

    >span {
        position: absolute;
        right: 0;
        top: 0;
        transform: translate(-50%, 0);
        font-size: 13px;
        color: #3c88c0;
    }

    >div {
        margin-top: 0px;
        display: flex;
        align-items: center;

        >img {
            width: 10px;
        }

        >div {
            display: flex;
            align-items: center;

            img {
                width: 20px;
            }

            p {
                font-size: 10px;
                color: #95999f;
            }
        }

    }
}

.about_hideen {
    overflow: hidden !important;
    text-overflow: ellipsis !important;
    white-space: nowrap !important;
}

.title_nav {
    position: sticky;
    top: 0px;
    background-color: white;
    padding: 0 0 10px 0;
    display: flex;
    justify-content: space-around;
    margin-top: 10px;
    border-top: 0.1px rgba(0, 0, 0, 0.13) solid;
    padding-top: 10px;

    a {
        font-size: 14px;
        color: #605f64;
    }
}


.router-link-active {
    color: #f68db5 !important;
}
</style>